//////////////////////////////////////////////////////////////////////////////////
//	Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "logviewer.h"
#include "guilogic.h"
#include "searchpanel.h"

#define LOG_W	600
#define LOG_H	600

enum cIDS
{
	ID_LIST=8000
};

static SEARCHFIELD sf[]=
{
	{ cSearchTypeString,	cLogColumnTime,		_("Time")	},
	{ cSearchTypeString,	cLogColumnSrc,		_("Source")	},
	{ cSearchTypeNumeric,	cLogColumnEvent,	_("Event")	},
};

BEGIN_EVENT_TABLE(CLogViewer, wxDialog)
	EVT_LIST_COL_CLICK(ID_LIST, CLogViewer::OnColumn)
END_EVENT_TABLE()

CLogViewer::CLogViewer(CGUILogic& logic):wxDialog(NULL, wxID_ANY, wxString( _("Application Log")),
											 wxDefaultPosition, wxSize(LOG_W, LOG_H),wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER ),
	CLogDataProvider(logic.GetLog()), m_logic(logic),
	CSearchImpl<CLogViewer>(*this)
{
	/*
	//Menu
	//File
	wxMenu *fileMenu = new wxMenu;
	fileMenu->Append(wxID_SAVE, _("Save"), _("Save") );

	//View
	wxMenu *viewMenu = new wxMenu;
	viewMenu->Append(wxID_CLEAR,  _("Clear"), _("Clear") );

	wxMenuBar* menuBar = new wxMenuBar;
	menuBar->Append(fileMenu, _("&File"));
	menuBar->Append(viewMenu, _("&View"));
	menuBar->Attach((wxFrame *)this);
	*/

	//Controls
	wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);

	m_pSearch = new CSearchPanel(this, this);
	sizer->Add(m_pSearch, 0, wxALL|wxEXPAND);

	m_nSortCol = sf[0].iFieldID;
	m_pList = new CLogCtrl<CLogViewer>(this, ID_LIST);

	SetSearchListPtr(m_pList);

	sizer->Add(m_pList, 1, wxALL|wxEXPAND);

	for (size_t i=0;i<sizeof(sf)/sizeof(sf[0]);i++)
	{
		m_pList->InsertColumn( sf[i].iFieldID, sf[i].strLabel );
	}

	m_pList->SetColumnWidth( cLogColumnTime,	120 );
	m_pList->SetColumnWidth( cLogColumnSrc,		100 );
	m_pList->SetColumnWidth( cLogColumnEvent,	260 );

	wxButton* pButton = new wxButton(this, wxID_OK, _("Close"), wxDefaultPosition, wxDefaultSize );
	Connect(wxEVT_COMMAND_BUTTON_CLICKED , wxCommandEventHandler(CLogViewer::OnCloseBtn));
	
	sizer->Add(pButton, 0, wxALL|wxEXPAND|wxALIGN_CENTER);

	SetSizer(sizer);
}

void CLogViewer::DataChanged()
{
	ResetSearchPos();

	((CLogCtrl<CLogViewer>*)m_pList)->DataChanged();
}

CLogViewer::~CLogViewer(void)
{
	m_logic.OnLogWndClosed();
}

//////////////////////////////////////////////////////////////////////////////////////////
void CLogViewer::OnCloseBtn(wxCommandEvent& evnt)
{
	Destroy();
}

void CLogViewer::DoSort()
{
	m_logic.SortLog( m_nSortCol );
}

void CLogViewer::OnColumn(wxListEvent& ev)
{
	CGUILogic::m_bSortAsc=!CGUILogic::m_bSortAsc;

	m_nSortCol = ev.m_col;

	DoSort();
	
	((CLogCtrl<CLogViewer>*)m_pList)->DataChanged();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Search/Filter
void CLogViewer::GetSearchFields(SEARCHFIELD*& sfRes, size_t& sz)
{
	sfRes = sf;
	sz = sizeof(sf)/sizeof(sf[0]);
}


void CLogViewer::OnFilterChanged()
{
}
